import { MenuCommandParams as Params } from '@swda/configs/params/index.js'
import { setGlobal } from '@swda/utils'


//-----------------------------------------------------------------------------
// SWDA_Window_MenuCommand
//
// The window for selecting a command on the menu screen.

function SWDA_Window_MenuCommand() {
    this.initialize.apply(this, arguments);

}

SWDA_Window_MenuCommand.prototype = Object.create(SWDA_Window_Command_4Arrows.prototype);
SWDA_Window_MenuCommand.prototype.constructor = SWDA_Window_MenuCommand;


SWDA_Window_MenuCommand.prototype.initialize = function (x, y) {
    SWDA_Window_Command_4Arrows.prototype.initialize.call(this, x, y);
    this.selectLast();
    this.createButtons(Params.commandMenuButtonImage);
};


SWDA_Window_MenuCommand._lastCommandSymbol = null;

SWDA_Window_MenuCommand.initCommandPosition = function () {
    this._lastCommandSymbol = null;
};

SWDA_Window_MenuCommand.prototype.windowWidth = function () {
    return 240;
};

SWDA_Window_MenuCommand.prototype.numVisibleRows = function () {
    return this.maxItems();
};

SWDA_Window_MenuCommand.prototype.makeCommandList = function () {
    this.addMainCommands();
};

SWDA_Window_MenuCommand.prototype.addMainCommands = function () {
    let enabled = this.areMainCommandsEnabled();
    enabled = this.isOptionsEnabled();
    this.addCommand(TextManager.options, 'tianshu', enabled);
    this.addCommand(TextManager.skill, 'skill', enabled);
    this.addCommand(TextManager.status, 'status', enabled);
    this.addCommand(TextManager.item, 'item', enabled);
};

// 主菜单是否可用（是否存在主角队伍）
SWDA_Window_MenuCommand.prototype.areMainCommandsEnabled = function () {
    return $gameParty.exists();
};

// "设置选项“是否可用（默认可用）
SWDA_Window_MenuCommand.prototype.isOptionsEnabled = function () {
    return true;
};

// 执行OK的内容
SWDA_Window_MenuCommand.prototype.processOk = function () {
    SWDA_Window_MenuCommand._lastCommandSymbol = this.currentSymbol();
    Window_Command.prototype.processOk.call(this);
};


// 上一次选择的命令
SWDA_Window_MenuCommand.prototype.selectLast = function () {
    this.selectSymbol(SWDA_Window_MenuCommand._lastCommandSymbol);
};

// 纠正initCommandPosition
Scene_Map.prototype.callMenu = function() {
    SoundManager.playOk();
    SceneManager.push(Scene_Menu);
    if(!ConfigManager.commandRemember)
        SWDA_Window_MenuCommand.initCommandPosition();
    $gameTemp.clearDestination();
    this._mapNameWindow.hide();
    this._waitCount = 2;
};

Game_Interpreter.prototype.command351 = function() {
    if (!$gameParty.inBattle()) {
        SceneManager.push(Scene_Menu);
        if(!ConfigManager.commandRemember)
            SWDA_Window_MenuCommand.initCommandPosition();
    }
    return true;
};

setGlobal('SWDA_Window_MenuCommand', SWDA_Window_MenuCommand)